﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;
using MySql.Data.MySqlClient;
using System.Data;
namespace BusinessFacade
{
    public class Busi_pm_StaffManage
    {
        public Busi_pm_StaffManage()
        {
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public bool Add(pm_StaffManage_Model model)
        {
            int rowsAffected;
            MySqlParameter[] parameters = {

            new MySqlParameter("psafeID", MySqlDbType.Int32,10),

            new MySqlParameter("psafeName", MySqlDbType.VarChar,200),

		    new MySqlParameter("psafeState", MySqlDbType.VarChar,100),

	        new MySqlParameter("pAge", MySqlDbType.Int32,10),

	        new MySqlParameter("pSex", MySqlDbType.VarChar,10),

            new MySqlParameter("ppaymentterm", MySqlDbType.Int32,10),

	        new MySqlParameter("parrearageterm", MySqlDbType.Int32,10),

            new MySqlParameter ("ppersonAccountNum", MySqlDbType.VarChar,100),

            new MySqlParameter ("ppaymentbase", MySqlDbType.Decimal,50),

            new MySqlParameter ("pConsume", MySqlDbType.Int32,10),

            new MySqlParameter ("paremID", MySqlDbType.Int32,10),

            new MySqlParameter ("pTelPhone", MySqlDbType.VarBinary,11),

            new MySqlParameter ("psafeCode", MySqlDbType.VarChar,10),

            new MySqlParameter ("pIDNumber", MySqlDbType.VarChar,18),

            new MySqlParameter ("pcreateDate", MySqlDbType.Date),

            new MySqlParameter ("pcreateManID", MySqlDbType.Int32,10),
                                          };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.safeName;
            parameters[2].Value = model.safeState;
            parameters[3].Value = model.Age;
            parameters[4].Value = model.Sex;
            parameters[5].Value = model.paymentterm;
            parameters[6].Value = model.arrearageterm;
            parameters[7].Value = model.personAccountNum;
            parameters[8].Value = model.paymentbase;
            parameters[9].Value = model.Consume;
            parameters[10].Value = model.aremID;
            parameters[11].Value = model.TelPhone;
            parameters[12].Value = model.safeCode;
            parameters[13].Value = model.IDNumber;
            parameters[14].Value = model.createDate;
            parameters[15].Value = model.createManID;

            DbHelperMySql.RunProcedure("pm_staffmanage_Add", parameters);
            rowsAffected = int.Parse(parameters[0].Value.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 插入映射表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool su_mapped_ADD(su_mapped_Model model)
        {
            int rowsAffected;
            MySqlParameter[] parameters = {

                    new MySqlParameter("pmapID", MySqlDbType.Int32,11),

					new MySqlParameter("PName", MySqlDbType.VarChar,150),

					new MySqlParameter("PMappedChar", MySqlDbType.VarChar,150),

					new MySqlParameter("pTelPhone", MySqlDbType.VarChar,254)
                 
                                          };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.MappedChar;
            parameters[3].Value = model.TelPhone;
            DbHelperMySql.RunProcedure("su_mapped_ADD", parameters);
            rowsAffected = int.Parse(parameters[0].Value.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            return false;
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(pm_StaffManage_Model model)
        {
            MySqlParameter[] parameters = {
                   
            new MySqlParameter("psafeID", MySqlDbType.Int32),

            new MySqlParameter("psafeName", MySqlDbType.VarChar,200),

		    new MySqlParameter("psafeState", MySqlDbType.VarChar,100),

	        new MySqlParameter("pAge", MySqlDbType.Int32),

	        new MySqlParameter("pSex", MySqlDbType.VarChar,10),

            new MySqlParameter("ppaymentterm", MySqlDbType.Int32),

	        new MySqlParameter("parrearageterm", MySqlDbType.Int32),

            new MySqlParameter ("ppersonAccountNum", MySqlDbType.VarChar,100),

            new MySqlParameter ("ppaymentbase", MySqlDbType.Decimal,50),

            new MySqlParameter ("pConsume", MySqlDbType.Int32),

            new MySqlParameter ("paremID", MySqlDbType.Int32),

            new MySqlParameter ("pTelPhone", MySqlDbType.VarBinary,11),

            new MySqlParameter ("psafeCode", MySqlDbType.VarChar,10),

            new MySqlParameter ("pIDNumber", MySqlDbType.VarChar,18),

            new MySqlParameter ("pupdateDate", MySqlDbType.Date),

            new MySqlParameter ("pupdateManID", MySqlDbType.Int32),
                                          };
            parameters[0].Value = model.safeID;
            parameters[1].Value = model.safeName;
            parameters[2].Value = model.safeState;
            parameters[3].Value = model.Age;
            parameters[4].Value = model.Sex;
            parameters[5].Value = model.paymentterm;
            parameters[6].Value = model.arrearageterm;
            parameters[7].Value = model.personAccountNum;
            parameters[8].Value = model.paymentbase;
            parameters[9].Value = model.Consume;
            parameters[10].Value = model.aremID;
            parameters[11].Value = model.TelPhone;
            parameters[12].Value = model.safeCode;
            parameters[13].Value = model.IDNumber;
            parameters[14].Value = model.updateDate;
            parameters[15].Value = model.updateManID;
            DbHelperMySql.RunProcedure("pm_staffmanage_Update", parameters);
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="PlanName"></param>
        /// <param name="MoubleID"></param>
        /// <returns></returns>
        public DataTable GetList(string PlanName, int MoubleID)
        {
            DataSet ds = new DataSet();
            MySqlParameter[] parameters = {
                    new MySqlParameter("safeCode", MySqlDbType.VarChar,10),
					new MySqlParameter("aremID", MySqlDbType.Int32,10)};
            parameters[0].Value = PlanName;
            parameters[1].Value = MoubleID;
            ds = DbHelperMySql.RunProcedure("pm_StaffManage_Select", parameters);
            return ds.Tables[0];
        }

        public DataSet GetDateSet(object id)
        {
            string sql = "select * from pm_StaffManage where safeID in(" + id + ")";
            DataSet ds = DbHelperMySql.Query(sql);
            return ds;
        }



        public DataSet Get_IDNumber(string pIDNumber, string safeCode)
        {
            string sql = "select * from pm_StaffManage where IDNumber =(" + pIDNumber + ") and safeCode ='" + safeCode + "'";
            DataSet ds = DbHelperMySql.Query(sql);
            return ds;
        }

        public DataSet Get_ID(int ID)
        {
            string sql = "select * from pm_StaffManage where safeID in(" + ID + ")";
            DataSet ds = DbHelperMySql.Query(sql);
            return ds;
        }


        /// <summary>
        /// 得到值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public pm_StaffManage_Model GetListModel(object id)
        {
            pm_StaffManage_Model model = new pm_StaffManage_Model();
            string sql = "select * from pm_StaffManage where safeID in(" + id + ")";
            DataSet ds = DbHelperMySql.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["safeID"] != null && ds.Tables[0].Rows[0]["safeID"].ToString() != "")
                {
                    model.safeID = int.Parse(ds.Tables[0].Rows[0]["safeID"].ToString());
                }

                if (ds.Tables[0].Rows[0]["safeName"] != null && ds.Tables[0].Rows[0]["safeName"].ToString() != "")
                {
                    model.safeName = ds.Tables[0].Rows[0]["safeName"].ToString();
                }

                if (ds.Tables[0].Rows[0]["Age"] != null && ds.Tables[0].Rows[0]["Age"].ToString() != "")
                {
                    model.Age = Convert.ToInt32((ds.Tables[0].Rows[0]["Age"]).ToString());
                }

                if (ds.Tables[0].Rows[0]["Sex"] != null && ds.Tables[0].Rows[0]["Sex"].ToString() != "")
                {
                    model.Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
                }

                if (ds.Tables[0].Rows[0]["safeState"] != null && ds.Tables[0].Rows[0]["safeState"].ToString() != "")
                {
                    model.safeState = ds.Tables[0].Rows[0]["safeState"].ToString();
                }

                if (ds.Tables[0].Rows[0]["personAccountNum"] != null && ds.Tables[0].Rows[0]["personAccountNum"].ToString() != "")
                {
                    model.personAccountNum = ds.Tables[0].Rows[0]["personAccountNum"].ToString();
                }

                if (ds.Tables[0].Rows[0]["paymentterm"] != null && ds.Tables[0].Rows[0]["paymentterm"].ToString() != "")
                {
                    model.paymentterm = Convert.ToInt32(ds.Tables[0].Rows[0]["paymentterm"].ToString());
                }

                if (ds.Tables[0].Rows[0]["arrearageterm"] != null && ds.Tables[0].Rows[0]["arrearageterm"].ToString() != "")
                {
                    model.arrearageterm = Convert.ToInt32(ds.Tables[0].Rows[0]["arrearageterm"].ToString());
                }

                if (ds.Tables[0].Rows[0]["paymentbase"] != null && ds.Tables[0].Rows[0]["paymentbase"].ToString() != "")
                {
                    model.paymentbase = Convert.ToDecimal(ds.Tables[0].Rows[0]["paymentbase"].ToString());
                }

                if (ds.Tables[0].Rows[0]["Consume"] != null && ds.Tables[0].Rows[0]["Consume"].ToString() != "")
                {
                    model.Consume = Convert.ToInt32(ds.Tables[0].Rows[0]["Consume"].ToString());
                }

                if (ds.Tables[0].Rows[0]["aremID"] != null && ds.Tables[0].Rows[0]["aremID"].ToString() != "")
                {
                    model.aremID = Convert.ToInt32(ds.Tables[0].Rows[0]["aremID"].ToString());
                }

                if (ds.Tables[0].Rows[0]["createManID"] != null && ds.Tables[0].Rows[0]["createManID"].ToString() != "")
                {
                    model.createManID = Convert.ToInt32(ds.Tables[0].Rows[0]["createManID"].ToString());
                }

                if (ds.Tables[0].Rows[0]["createDate"] != null && ds.Tables[0].Rows[0]["createDate"].ToString() != "")
                {
                    model.createDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["createDate"].ToString());
                }

                if (ds.Tables[0].Rows[0]["updateManID"] != null && ds.Tables[0].Rows[0]["updateManID"].ToString() != "")
                {
                    model.updateManID = Convert.ToInt32(ds.Tables[0].Rows[0]["updateManID"].ToString());
                }

                if (ds.Tables[0].Rows[0]["updateDate"] != null && ds.Tables[0].Rows[0]["updateDate"].ToString() != "")
                {
                    model.updateDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["updateDate"].ToString());
                }

                if (ds.Tables[0].Rows[0]["TelPhone"] != null && ds.Tables[0].Rows[0]["TelPhone"].ToString() != "")
                {
                    model.TelPhone = ds.Tables[0].Rows[0]["TelPhone"].ToString();
                }

                if (ds.Tables[0].Rows[0]["safeCode"] != null && ds.Tables[0].Rows[0]["safeCode"].ToString() != "")
                {
                    model.safeCode = ds.Tables[0].Rows[0]["safeCode"].ToString();
                }

                if (ds.Tables[0].Rows[0]["IDNumber"] != null && ds.Tables[0].Rows[0]["IDNumber"].ToString() != "")
                {
                    model.IDNumber = ds.Tables[0].Rows[0]["IDNumber"].ToString();
                }
                return model;
            }
            model = null;
            return model;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            MySqlParameter[] parameters = { 
                             new MySqlParameter("psafeID",MySqlDbType.Int32)
                                         };
            parameters[0].Value = id;
            DbHelperMySql.RunProcedure("pm_staffmanage_Delete", parameters);
        }
    }
}
